package com.gulimall.search.controller;

import com.gulimall.common.exception.BizCodeException;
import com.gulimall.common.utils.R;
import com.gulimall.search.service.ElasticSearchSaveService;
import com.gulimall.search.to.product.SkuEsModelTo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @Author by董江
 * @Tool Intellij IDEA
 * @Classname ElasticSearchSaveController
 * @Date 2022/5/11 15:08
 */
@Log4j2
@RestController
@RequestMapping("/search/save")
public class ElasticSearchSaveController {
    @Autowired
    private ElasticSearchSaveService elasticSearchSaveService;

    /**
     * 产品上架
     * @param skuEsModelTos
     * @return
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModelTo> skuEsModelTos) {
        boolean b = false;
        try {
            b = elasticSearchSaveService.productStatusUp(skuEsModelTos);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("远程调用商品上架发生错误：{} 请求路径：{}",e,"/search/save/product");
            return R.error(BizCodeException.PRODUCT_UP_EXCEPTION.getCode(), BizCodeException.PRODUCT_UP_EXCEPTION.getMsg());
        }

        if (b) {
            return R.ok();
        }else {
            return R.error(BizCodeException.PRODUCT_UP_EXCEPTION.getCode(), BizCodeException.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }
}
